package www.admin.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import www.admin.model.Operation;
import www.admin.model.User;
import www.admin.service.IOperationService;


/**
 * Base class for controller, though sub controller should define {@link
 * @Controller} itself.
 * 
 * 
 * 
 */
public abstract class BaseController {

	/** log instance. */
	protected final Logger log = LoggerFactory.getLogger(getClass());

	protected final int RETURN_STRING = 1;

	protected final int RETURN_MAP = 2;

	protected final String RETURN_JSONRESULT = "4";
	
	protected final int RETURN_JSONFOUR =3;
	
	protected final int RETURN_JSONFIVE = 5;
	
	private static int sequence = 100000; 
	
	protected final String RETURN_NUMBER_1 = "1";
	
	protected final String RETURN_NUMBER_3 = "3";
	
	protected final String RETURN_NUMBER_5 = "5";
	
	protected final String RETURN_NUMBER_NAME = "超级管理员";

	@Autowired
	private IOperationService operationService;
	
	/*private IUserInfoService service;*/
	
	public String getIpAddressFromReuqest(HttpServletRequest request){
		String ip = request.getHeader(" x-forwarded-for ");
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader(" Proxy-Client-IP ");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader(" WL-Proxy-Client-IP ");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	public String getRequestParam(String column, HttpServletRequest request) {
		Object ob = request.getParameterMap().get(column);
		if (ob == null || ob.equals("")) {
			return null;
		} else {
			String s[] = (String[]) ob;
			String columnStr = s[0];
			if (columnStr != null && !"".equals(columnStr.trim())) {
				return columnStr;
			} else {
				return null;
			}
		}
	}
		
	public String getIp(HttpServletRequest request) {
		String ip = request.getHeader(" x-forwarded-for ");
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader(" Proxy-Client-IP ");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader(" WL-Proxy-Client-IP ");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		
		return ip;
	}
	
	
	public void storeUserIntoSession(HttpServletRequest request, User user) {
		String ip = request.getHeader(" x-forwarded-for ");
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader(" Proxy-Client-IP ");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getHeader(" WL-Proxy-Client-IP ");
		}
		if (ip == null || ip.length() == 0 || " unknown ".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		request.getSession(true).setAttribute("user", user);
		request.getSession().setMaxInactiveInterval(21600);
	}
	
	public String getUserId(HttpServletRequest request){
		User user = (User)request.getSession(true).getAttribute("user");
		 if (user != null) {
			 return user.getUserId();
		 }
		return null;
	}
	
	public String getUserName(HttpServletRequest request){
		User user = (User)request.getSession(true).getAttribute("user");
		if (user != null) {
			return user.getUserName();
		}
		return null;
	} 
	
	public String getRoleId(HttpServletRequest request){
		User user = (User)request.getSession(true).getAttribute("user");
		if (user != null) {
			return user.getRoleId();
		}
		return null;
	} 
	
	public String getRoleName(HttpServletRequest request){
		User user = (User)request.getSession(true).getAttribute("user");
		if (user != null) {
			return user.getRoleName();
		}
		return null;
	} 
	
	public List<String> getResourceId(String resource) {
		List<String> list = new ArrayList<String>();
		String[] split = resource.split(",");
		for (int i = 0; i < split.length; i++) {
			list.add(split[i]);
		}
		return list;
	}
	
    /**  
     * 自动生成主键id
     * @return  
     */    
    public static synchronized String getSeqNum() {    
        sequence = sequence >= 999999 ? 1 : sequence + 1;    
        String datetime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        return datetime + sequence;  
    }
    
    public String insertOpera(String operationRecord){
    	HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    	Operation operation = new Operation();
    	operation.setId(getSeqNum());
    	operation.setUserName(getUserName(request));
    	operation.setPosition(getRoleName(request));
    	operation.setIp(getIp(request));
    	operation.setOperationRecord(operationRecord);
    	operation.setCreateTime(new Date());
    	return operationService.insert(operation);
    }
    
/*    public List<String> getUserInfo(HttpServletRequest request){
    	return service.getUserInfo(getUserId(request));
    }*/
	
}
